<template>
	<view class="login">
		<view class="img-a"></view>
		<view class="login-view">
			<view class="t-login">
				<view class="t-b">登 录</view>
				<u-form :model="loginForm" ref="loginForm" label-position="top">
					<u-form-item label="用户名" label-width='110' prop="username">
						<u-input v-model="loginForm.username" placeholder="请输入用户名或手机号" />
					</u-form-item>
					<u-form-item label="密码" label-width='110' prop="password">
						<u-input v-model="loginForm.password" type="password" placeholder="请输入密码" />
					</u-form-item>
				</u-form>
				<view class="loginBtn">
					<u-button @click="handleLogin" :ripple="true" shape="circle" type="primary">登录</u-button>
				</view>
			</view>
		</view>
		<image class="img-b" src="/static/images/bg2.png"></image>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				loginForm: {
					username: '',
					password: ''
				},
				rules: {
					username: [{
						required: true,
						message: '请输入用户名或手机号',
						// 可以单个或者同时写两个触发验证方式 
						trigger: ['blur'],
					}],
					password: [{
						required: true,
						min: 6,
						message: '请输入密码，最少为6位',
						trigger: ['blur']
					}]
				}
			};
		},
		onLoad() {},
		onReady() {
			this.$refs.loginForm.setRules(this.rules);
		},
		methods: {
			//当前登录按钮操作
			handleLogin() {
				this.$refs.loginForm.validate(async valid => {
					if (valid) {
						try {
							let loginRes = await this.$u.api.login(this.loginForm);
							console.log(loginRes);
							if (loginRes.statusCode === 200) {
								// 保存access-token到vuex
								this.$u.vuex('vuex_access_token', loginRes.header['Access-Token']);
								// 将Authorization 存入 storage 中
								uni.setStorageSync('access-token', loginRes.header['Access-Token']);
								this.getUserInfo();
								// this.getMenuList();
							} else {
								this.$refs.uToast.show({
									title: loginRes.data.msg,
									type: 'error',
								})
							}
						} catch (e) {
							console.log('login err：', e);
						}
					}
				});
			},
			// 获取用户信息
			async getUserInfo() {
				try {
					let res = await this.$u.api.getUserInfo();
					// 将用户信息存入store
					this.$u.vuex('vuex_user', res.data.data);
					// 跳转到首页
					uni.switchTab({
						url: '/pages/home/home'
					})
				} catch (e) {
					console.log('getUserInfo err：', e);
				};
			},
			// 获取用户权限菜单
			async getMenuList() {
				try {
					let res = await this.$u.api.getMenuList();
					this.$u.vuex('vuex_menuList', res.data.data);
					// 跳转到首页
					uni.switchTab({
						url: '/pages/home/home'
					})
				} catch (e) {
					console.log('getMenuList err：', e);
				}
			},
		}
	};
</script>

<style lang="scss" scoped>
	.login {
		width: 100vw;
		height: 100vh;
		background-image: linear-gradient(to top, #4481eb 0%, #04befe 100%);
		position: relative;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.img-b {
		width: 100%;
		height: 45px;
		bottom: 0;
		position: absolute;
	}

	.txt {
		font-size: 32rpx;
		font-weight: bold;
		color: #333333;
	}

	.img-a {
		width: 100%;
		height: 450rpx;
		background-image: url('/static/images/bg1.png');
		background-size: 100%;
		position: absolute;
		top: 0;
		left: 0;
	}

	.reg {
		font-size: 28rpx;
		color: #fff;
		height: 90rpx;
		line-height: 90rpx;
		border-radius: 50rpx;
		font-weight: bold;
		background: #f5f6fa;
		color: #000000;
		text-align: center;
		margin-top: 30rpx;
	}

	.login-view {
		width: 350px;
		padding: 27px;
		margin: 0 auto;
		font-size: 14px;
		background-color: #ffffff;
		border-radius: 10px;
		box-shadow: 0 5px 7px 0 rgba(0, 0, 0, 0.15);
		z-index: 9;
	}

	.t-login {
		width: 600rpx;
		margin: 0 auto;
		font-size: 28rpx;
	}

	.loginBtn {
		margin-top: 40rpx;
	}

	.t-login button {
		font-size: 28rpx;
		background: #2796f2;
		color: #fff;
		height: 90rpx;
		line-height: 90rpx;
		border-radius: 50rpx;
		font-weight: bold;
	}

	.t-b {
		text-align: left;
		font-size: 46rpx;
		color: #04befe;
		font-weight: bold;
	}
</style>
